package com.mall.mallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mall.mallproduct.entity.ProductAttrValueEntity;
import com.mall.mallproduct.service.ProductAttrValueService;
import com.mall.mallproduct.vo.AttrRespVO;
import com.mall.mallproduct.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.mallproduct.entity.AttrEntity;
import com.mall.mallproduct.service.AttrService;
import com.common.utils.PageUtils;
import com.common.utils.R;



/**
 * 商品属性
 *
 * @author zxc
 * @email zxc@gmail.com
 * @date 2020-08-13 17:55:40
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return R.ok().put("data",entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@RequestParam Map<String, Object> params
            ,@PathVariable("catelogId") Long catelogId
            ,@PathVariable("attrType") String type){
        PageUtils page = attrService.queryPageBaseAttrPage(params,catelogId,type);
        return R.ok().put("page",page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
   // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
       AttrRespVO respVO= attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attrVO){
		//attrService.save(attrVO);
        attrService.saveAttrVO(attrVO);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attrVO){
		//attrService.updateById(attr);
        attrService.updateAttr(attrVO);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
